import React, { Component } from 'react'
import { NavLink, Route, Switch, Redirect } from 'react-router-dom'

export default class RootRouter extends Component {
    render() {
        const { routes } = this.props
        return (
            <div>
                {
                    routes.map((item, index) => {
                        return <Route path={item.path}
                            render={() => {
                                // 添加拦截功能
                                if (item.auth && !sessionStorage.getItem('token')) {
                                    return <Redirect to={{
                                        pathname: "/login",
                                        // 记录要拦截的页面
                                        goBack: item.path
                                    }} />
                                } else {
                                    const Com = item.component
                                    if (item.children) {
                                        return <Com  routeChildren={item.children} />
                                    } else {
                                        return <Com />
                                    }
                                }
                            }}
                            key={index}
                        />
                    })
                }
            </div>
        )
    }
}
